/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;
import util.HibernateUtil;
import java.util.List;
import pojo.Danhmuccon;
import org.hibernate.Query;
import org.hibernate.Session;
/**
 *
 * @author Jackhit
 */
public class DanhMucConDAO {
    public static  List<Danhmuccon> layDanhSachDanhMucCon() {
        List<Danhmuccon> dsDanhMucCon = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Danhmuccon";
            Query query = session.createQuery(hql);
            dsDanhMucCon = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsDanhMucCon;
    }

    public static  List<Danhmuccon> layDanhSachDanhMucConTheoId(int ma) {
        List<Danhmuccon> dsDanhMucCon = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Danhmuccon where danhmuc.maDanhMuc = :ma";
            Query query = session.createQuery(hql);
            query.setString("ma", ""+ma);
            dsDanhMucCon = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsDanhMucCon;
    }

    public static  boolean themDanhMucCon(Danhmuccon danhMucCon) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(danhMucCon);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static  boolean capNhatDanhMucCon(Danhmuccon danhMucCon) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(danhMucCon);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static  boolean xoaDanhMucCon(Danhmuccon danhMucCon) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(danhMucCon);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }
}
